package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import modelo.diagrama.de.classes.Classe;
import modelo.diagrama.de.classes.Composicao;
import modelo.diagrama.de.classes.Nota;
import modelo.diagramas.ElementoDeModelo;
import modelo.estado.StateElementoDeModeloSelecionado;
import modelo.estado.StateNenhumSelecionado;
import controlador.Controlador;

@SuppressWarnings("serial")
public class PalcoPintura extends JPanel {

	// Singleton
	private static PalcoPintura instance;

	// Scroll para o PalcoPintura
	JScrollPane scroll = new JScrollPane();

	
	private PalcoPintura() {

		setPreferredSize(new Dimension(
				Controlador.getInstance().LARGURA_MAXIMA_PALCO_PINTURA,
				Controlador.getInstance().ALTURA_MAXIMA_PALCO_PINTURA));
		setBackground(Color.WHITE);
		scroll.setViewportView(this);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		this.addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent e) {

			}

			@Override
			public void mouseDragged(MouseEvent e) {
				Controlador.getInstance().atualizaPosicaoElementoSelecionado(e.getX(),
						e.getY());
				repaint();
			}
		});

		this.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {

			}

			@Override
			public void mousePressed(MouseEvent arg0) {

			}

			@Override
			public void mouseExited(MouseEvent arg0) {

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {

			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				Controlador.getInstance().testaSelecaoPinta(arg0.getX(), arg0.getY());
				Controlador.getInstance().getEstado().adicionaElementoDeModelo(arg0.getX(),
						arg0.getY(),
						Controlador.getInstance().getOrigemSelecionado(),
						Controlador.getInstance().getDestinoSelecionado());
				repaint();
			}
		});

	}

	public static PalcoPintura getInstance() {
		if (instance == null) {
			instance = new PalcoPintura();
		}
		return instance;
	}

	// Pinta todos os componentes no palcoPintura.
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (ElementoDeModelo oG : Controlador.getInstance()
				.getElementosDeModeloAdicionados()) {
			oG.pinta(g);
		}
	}

}
